package com.junqi.media.service.impl;

import cn.hutool.core.util.StrUtil;
import com.junqi.media.hls.HlsHandler;
import com.junqi.media.service.HlsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
@Service
public class HlsServiceImpl implements HlsService {

    /**
     * hls切片地址
     */
    @Value(value = "${jq.file_path}")
    private String filePath="";

    @Autowired
    HlsHandler hlsHandler;

    /**
     * 关闭hls切片
     *
     * @param streamKey
     */
//    @Override
//    public void start(String streamKey) {
//        hlsHandler.startHls(streamKey);
//    }
//    @Override
//    public void end(String streamKey) {
//        hlsHandler.endHls(streamKey);
//    }

    /**
     * hls播放接口 获取ts文件
     * @throws IOException
     */
    @Override
    public void getTsFile(HttpServletResponse response, String roomId, String tsName) throws IOException {
        Path videoPath = Paths.get(filePath, roomId + File.separator + tsName);
        // 检查文件是否存在且可读
        Resource resource = new UrlResource(videoPath.toUri());
        if (resource.exists() || resource.isReadable()) {
            //response.setHeader("Access-Control-Allow-Origin", "*"); // 或指定前端地址
            response.getOutputStream().write(Files.readAllBytes(videoPath));
            response.setContentType("video/mp2t");
            response.getOutputStream().flush();
        } else {
            //response.setHeader("Access-Control-Allow-Origin", "*"); // 或指定前端地址
            response.setContentType("application/json");
            response.getOutputStream().write("尚未生成ts".getBytes("utf-8"));
            response.getOutputStream().flush();
            return;
        }
    }

    /**
     * hls播放接口 获取m3u8文件
     * @throws IOException
     */
    @Override
    public void video(String roomId, HttpServletResponse response) throws IOException {
        if (StrUtil.isBlank(roomId)) {
            response.setHeader("Access-Control-Allow-Origin", "*"); // 或指定前端地址
            response.setContentType("application/json");
            response.getOutputStream().write("参数有误".getBytes("utf-8"));
            response.getOutputStream().flush();
        } else {
            Path videoPath = Paths.get(filePath, roomId+ File.separator +"video.m3u8");
            // 检查文件是否存在且可读
            Resource resource = new UrlResource(videoPath.toUri());
            if (resource.exists() || resource.isReadable()) {
                //response.setHeader("Access-Control-Allow-Origin", "*"); // 或指定前端地址
                response.setContentType("application/vnd.apple.mpegurl");// application/x-mpegURL //video/mp2t ts;
                response.getOutputStream().write(Files.readAllBytes(videoPath));
                response.getOutputStream().flush();
            } else {
                response.setContentType("application/json");
                //response.setHeader("Access-Control-Allow-Origin", "*"); // 或指定前端地址
                response.getOutputStream().write("尚未生成m3u8".getBytes("utf-8"));
                response.getOutputStream().flush();
            }
        }
    }

}
